//
//  ExamTread.m
//  ocdemo
//
//  Created by JIENING ZHANG on 2020/5/10.
//  Copyright © 2020 JIENING ZHANG. All rights reserved.
//

#import "ExamTread.h"

@implementation ExamTread


// 4个任务ABCD
// C依赖AB完成，期望尽量快执行完成
// D和ABC无关，期望尽量快执行完成所有任务

// ABCD 4个任务

+ (void)taskA:(NSString *)desc {
    NSLog(@"taskA begin, %@, %@", desc, [NSThread currentThread]);
    [NSThread sleepForTimeInterval:3.0];
    NSLog(@"taskA end, %@, %@", desc, [NSThread currentThread]);
}

+ (void)taskB:(NSString *)desc {
    NSLog(@"taskB begin, %@, %@", desc, [NSThread currentThread]);
    [NSThread sleepForTimeInterval:1.0];
    NSLog(@"taskB end, %@, %@", desc, [NSThread currentThread]);
}

+ (void)taskC:(NSString *)desc {
    NSLog(@"taskC begin, %@, %@", desc, [NSThread currentThread]);
    [NSThread sleepForTimeInterval:1.0];
    NSLog(@"taskC end, %@, %@", desc, [NSThread currentThread]);
}

+ (void)taskD:(NSString *)desc {
    NSLog(@"taskD begin, %@, %@", desc, [NSThread currentThread]);
    [NSThread sleepForTimeInterval:1.0];
    NSLog(@"taskD end, %@, %@", desc, [NSThread currentThread]);
}


+ (void)run4TaskNSThreadLock {
    // 4个任务ABCD
    // C依赖AB完成，使用 NSThread NSLock 实现
    // D和ABC无关，期望尽量快执行完成所有任务
    
    NSLock *lA = [[NSLock alloc]init];

    NSThread *tA = [[NSThread alloc]initWithBlock:^{
        [ExamTread taskA:@"Thread+lock"];
        
        [lA unlock];
    }];
    
    NSLock *lB = [[NSLock alloc]init];

    NSThread *tB = [[NSThread alloc]initWithBlock:^{
        [ExamTread taskB:@"Thread+lock"];
        
        [lB unlock];
    }];
    
    NSThread *tC = [[NSThread alloc]initWithBlock:^{
        
        [lA lock];
        [lB lock];
        
        [ExamTread taskC:@"Thread+lock"];
        
        [lB unlock];
        [lA unlock];
    }];
    
    NSThread *tD = [[NSThread alloc]initWithBlock:^{
        [ExamTread taskD:@"Thread+lock"];
    }];
    
    [lA lock];
    [tA start];
    
    [lB lock];
    [tB start];
    
    [tC start];
    [tD start];
}

+ (void)run4TaskNSThreadSemaphore {
    // 4个任务ABCD
    // C依赖AB完成，使用 NSThread dispatch_semaphore 实现
    // D和ABC无关，期望尽量快执行完成所有任务
    
    dispatch_semaphore_t sa = dispatch_semaphore_create(0);
    NSThread *tA = [[NSThread alloc]initWithBlock:^{
        [ExamTread taskA:@"Thread+Semaphore"];
        
        dispatch_semaphore_signal(sa);
    }];
    
    dispatch_semaphore_t sb = dispatch_semaphore_create(0);
    NSThread *tB = [[NSThread alloc]initWithBlock:^{
        [ExamTread taskB:@"Thread+Semaphore"];
        
        dispatch_semaphore_signal(sb);
    }];
    
    NSThread *tC = [[NSThread alloc]initWithBlock:^{
        
        dispatch_semaphore_wait(sa, DISPATCH_TIME_FOREVER);
        dispatch_semaphore_wait(sb, DISPATCH_TIME_FOREVER);
        
        [ExamTread taskC:@"Thread+Semaphore"];
    }];
    
    NSThread *tD = [[NSThread alloc]initWithBlock:^{
        [ExamTread taskD:@"Thread+Semaphore"];
    }];
    
    [tA start];
    [tB start];
    [tC start];
    [tD start];
}


+ (void)run4TaskOperation {
    // 4个任务ABCD
    // C依赖AB完成，使用 NSOperation NSOperationQueue 实现
    // D和ABC无关，期望尽量快执行完成所有任务
    
    NSOperationQueue *queue=[[NSOperationQueue alloc] init];
        //创建操作
    NSBlockOperation *operation1=[NSBlockOperation blockOperationWithBlock:^(){
        [ExamTread taskA:@"run4TaskOperation"];
    }];
    NSBlockOperation *operation2=[NSBlockOperation blockOperationWithBlock:^(){
        [ExamTread taskB:@"run4TaskOperation"];
    }];
    NSBlockOperation *operation3=[NSBlockOperation blockOperationWithBlock:^(){
        [ExamTread taskC:@"run4TaskOperation"];
    }];
    NSBlockOperation *operation4=[NSBlockOperation blockOperationWithBlock:^(){
        [ExamTread taskD:@"run4TaskOperation"];
    }];
    
    [operation3 addDependency:operation1];
    [operation3 addDependency:operation2];
        //将操作添加到队列中去
    [queue addOperation:operation1];
    [queue addOperation:operation2];
    [queue addOperation:operation3];
    [queue addOperation:operation4];
}


+ (void)run4TaskGCD {
    // 4个任务ABCD
    // C依赖AB完成，使用 GCD 实现
    // D和ABC无关，期望尽量快执行完成所有任务
    
    dispatch_queue_t queue = dispatch_queue_create("aaa", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        [ExamTread taskA:@"run4TaskGCD"];
    });
    
    dispatch_async(queue, ^{
        [ExamTread taskB:@"run4TaskGCD"];
    });
    
    dispatch_barrier_async(queue, ^{
        [ExamTread taskC:@"run4TaskGCD"];
    });
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [ExamTread taskD:@"run4TaskGCD"];
    });
}


+ (void)run4Task {
    // 4个任务ABCD
    // C依赖AB
    // D和ABC无关，期望尽量快执行完成所有任务
    // any your idea
}

+ (void)demoLogic{
    //[ExamTread run4TaskNSThreadLock];
    [ExamTread run4TaskNSThreadSemaphore];
    //[ExamTread run4TaskOperation];
    //[ExamTread run4TaskGCD];
    //[ExamTread run4Task];
}

 
@end
